import {
  Body,
  Controller,
  Delete,
  Get,
  Logger,
  Param,
  Post,
  Put
} from '@nestjs/common';
import { CreateUserDTO, EditUserDTO } from './user.dto';
import { User } from './user.interface';
import { UserService } from './user.service';
import { CommonResult } from 'src/common/CommonResult';
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';

@ApiTags('用户接口')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @ApiOperation({ summary: '查询用户列表' })
  @ApiResponse({
    status: 200,
    description: '成功获取用户列表',
    example: {
      code: '1',
      message: 'success',
      data: [
        {
          _id: '1',
          user_name: '小熊',
          password: '111',
          __v: 0
        },
        {
          _id: '2',
          user_name: '小狼',
          password: '123456',
          __v: 0
        }
      ]
    }
  })
  @Get('list')
  async findAll(): Promise<CommonResult<User[]>> {
    try {
      const data = await this.userService.findAll();
      return CommonResult.successWithData(data);
    } catch (error) {
      return CommonResult.fail('查询失败');
    }
  }

  @ApiOperation({ summary: '查询单个用户' })
  @ApiParam({ name: '_id', description: '用户的唯一标识符', required: true })
  @ApiResponse({
    status: 200,
    description: '成功获取单个用户信息',
    example: {
      code: '1',
      message: 'success',
      data: {
        _id: '1',
        user_name: '小熊',
        password: '111',
        __v: 0
      }
    }
  })
  @Get(':_id')
  async findOne(@Param('_id') _id: string): Promise<CommonResult<User>> {
    try {
      const data = await this.userService.findOne(_id);
      return CommonResult.successWithData(data);
    } catch (error) {
      return CommonResult.fail('查询失败');
    }
  }

  @ApiOperation({ summary: '新增单个用户' })
  @ApiResponse({
    status: 200,
    description: '新增成功',
    example: {
      code: '1',
      message: '新增成功',
      data: null
    }
  })
  @Post()
  async addOne(@Body() body: CreateUserDTO): Promise<CommonResult<boolean>> {
    try {
      const result = await this.userService.addOne(body);
      return result
        ? CommonResult.successByMessage('新增成功')
        : CommonResult.fail('新增失败');
    } catch (error) {
      Logger.error(error);
      return CommonResult.error('服务异常，操作失败', error?.message);
    }
  }

  @ApiOperation({ summary: '修改单个用户' })
  @ApiParam({ name: '_id', description: '用户的唯一标识符', required: true })
  @ApiResponse({
    status: 200,
    description: '修改成功',
    example: {
      code: '1',
      message: '修改成功',
      data: null
    }
  })
  @Put(':_id')
  async editOne(
    @Param('_id') _id: string,
    @Body() body: EditUserDTO
  ): Promise<CommonResult<boolean>> {
    try {
      const result = await this.userService.editOne(_id, body);
      return result
        ? CommonResult.successByMessage('修改成功')
        : CommonResult.fail('修改失败');
    } catch (error) {
      Logger.error(error);
      return CommonResult.error('服务异常，操作失败', error?.message);
    }
  }

  @ApiOperation({ summary: '删除单个用户' })
  @ApiParam({ name: '_id', description: '用户的唯一标识符', required: true })
  @ApiResponse({
    status: 200,
    description: '删除成功',
    example: {
      code: '1',
      message: '删除成功',
      data: null
    }
  })
  @Delete(':_id')
  async deleteOne(@Param('_id') _id: string): Promise<CommonResult<boolean>> {
    try {
      const result = await this.userService.deleteOne(_id);
      return result
        ? CommonResult.successByMessage('删除成功')
        : CommonResult.fail('删除失败');
    } catch (error) {
      Logger.error(error);
      return CommonResult.error('服务异常，操作失败', error?.message);
    }
  }
}
